/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesoDatos;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import logicaNegocio.Prestamo;
import oracle.jdbc.driver.OracleTypes;

/**
 *
 * @author Manuelita
 */
public class GestorPrestamo extends Conexion implements Gestor<Prestamo> {
    private static final String ingresarPrestamo="";
    private static final String eliminarPrestamo="";
    private static final String modificarPrestamo="";
    private static final String listarPrestamo="";
    private static final String consultarPrestamo="";

    @Override
    public void ingresarDato(Prestamo clase) {
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(ingresarPrestamo);
            cs.setString(1, clase.getIdentificacion());
            cs.setInt(2, clase.getNumeroMaquina());
            cs.setInt(3, clase.getEstado());
            cs.setString(4, clase.getFechaInicio());
            cs.setString(5, clase.getFechaFinal());
            cs.setString(6, clase.getMotivo());
            boolean resultado = cs.execute();
            if(resultado){
                throw null;
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void eliminarDato(String identificacion) {
        CallableStatement ps = null;
        try{
            if(!this.Conectar())
                return;
            ps = conexion.prepareCall(eliminarPrestamo);
            ps.setString(1, identificacion);
            int resultado = ps.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(ps == null)
                    ps.close();
                this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void modificarDato(Prestamo clase) {
        CallableStatement ps = null;
        try{
            if(this.Conectar())
                return;
            ps = this.conexion.prepareCall(modificarPrestamo);
            ps.setString(1, clase.getIdentificacion());
            ps.setInt(2, clase.getNumeroMaquina());
            ps.setInt(3, clase.getEstado());
            ps.setString(4, clase.getFechaInicio());
            ps.setString(5, clase.getFechaFinal());
            ps.setString(6, clase.getMotivo());
            int resultado = ps.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
            if(ps != null)
                ps.close();
            this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public ArrayList<Prestamo> listarDato() {
        ResultSet rs = null;
        ArrayList<Prestamo> coleccion = new ArrayList<Prestamo>();
        Prestamo prestamo = null;
        CallableStatement cs = null;
        try{
            if(!this.Conectar())
                return null;
            cs = this.conexion.prepareCall(listarPrestamo);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                prestamo = new Prestamo(
                        rs.getString(1),
                        rs.getInt(2),
                        rs.getInt(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6)
                        );
                coleccion.add(prestamo);
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                this.Desconectar();
                if(coleccion == null || coleccion.isEmpty())
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
        }
        return coleccion;
    }

    @Override
    public Prestamo consultarDato(String identificacion) {
        ResultSet rs = null;
        Prestamo prestamo = null;
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(consultarPrestamo);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setString(2, identificacion);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                 prestamo = new Prestamo(
                         rs.getString(1),
                         rs.getInt(2),
                         rs.getInt(3),
                         rs.getString(4),
                         rs.getString(5),
                         rs.getString(6)
                        );
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                Desconectar();
                if(prestamo == null)
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getMessage());
                return null;
            }
        }
        return prestamo;
    }
}
